#!/usr/bin/env bash
. ./scripts/devcontainer/_assert-in-container "$0" "$@"

set -euo pipefail

# Tell honeycomb about a deploy

LOCAL_USERNAME="$(grep 'account' ~/.config/gcloud/configurations/config_default | awk '{print $3}' | awk -F "@" '{print $1}')"

if [[ -v CI ]]; then
  key="${HONEYCOMB_EVENT_KEY}"
  msg="${CIRCLE_USERNAME} deployed PR (SHA1: ${CIRCLE_SHA1})"
  url="${CIRCLE_BUILD_URL}"
else
  key="${DARK_CONFIG_HONEYCOMB_API_KEY}"
  msg="${1:-no msg}"
  msg="manual deploy by ${LOCAL_USERNAME} ${msg}"
  url=""
fi

# https://docs.honeycomb.io/api/datasets/
datasets=$( \
  curl https://api.honeycomb.io/1/datasets \
    -s \
    -X GET \
    -H "X-Honeycomb-Team: ${key}" \
  | jq .[].slug -r)

for dataset in $datasets; do
  honeymarker add \
      --writekey="${key}" \
      --dataset="$dataset" \
      --msg="${msg}" \
      --url="${url}" \
      --type="deploy"
done

echo "Honeycomb notified."